package com.xinchuang.service;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSON;
import com.xinchuang.entity.IndexCompute.FixedScore;
import com.xinchuang.entity.IndexCompute.OneFieldRange;
import com.xinchuang.entity.IndexCompute.OneFieldRangeValueData;
import com.xinchuang.entity.targetSystem.target.SystemIndex;
import com.xinchuang.entity.targetSystem.target.SystemIndexElse;
import com.xinchuang.service.targetSystem.target.SystemIndexElseService;
import com.xinchuang.service.targetSystem.target.SystemIndexService;
import com.xinchuang.util.FileElseUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.sf.jsqlparser.statement.create.table.ColDataType;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * TODO
 *
 * @author zkwczx
 * @version 1.0
 * @date 2024/4/26 9:32
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class OldDataService {

    private final SystemIndexService systemIndexService;

    private final SystemIndexElseService systemIndexElseService;

    public void oldToNewData() {
        List<SystemIndex> systemIndices = systemIndexService.lambdaQuery().isNotNull(SystemIndex::getIndicatorType).list();
        if (CollectionUtil.isNotEmpty(systemIndices)) {
            for (SystemIndex systemIndex : systemIndices) {
                if (StringUtils.isNotBlank(systemIndex.getMsgType())) {
                }
                if (StringUtils.isEmpty(systemIndex.getIndicatorType())) {
                } else {
                    if (systemIndex.getIndicatorType().contains("[")) {
                        List<SystemIndexElse> indexElses = systemIndexElseService.lambdaQuery().eq(SystemIndexElse::getRedId, systemIndex.getId()).list();
                        OneFieldRange oneFieldRange = new OneFieldRange();
                        oneFieldRange.setField(FileElseUtil.getObjectToList(systemIndex.getIndicatorType()));
                        if (CollectionUtil.isNotEmpty(indexElses)) {
                            List<OneFieldRangeValueData> list = new ArrayList<>();
                            for (SystemIndexElse indexEls : indexElses) {
                                OneFieldRangeValueData data = new OneFieldRangeValueData();
                                data.setMinValue(FileElseUtil.getObjectToDouble(indexEls.getPreposition()));
                                data.setMaxValue(FileElseUtil.getObjectToDouble(indexEls.getPostposition()));
                                data.setScore(FileElseUtil.getObjectToDouble(indexEls.getScore()));
                                list.add(data);
                            }
                            oneFieldRange.setValueDataList(list);
                        }
                        systemIndex.setMsgType("oneFieldRange");
                        systemIndex.setIndexMsg(JSON.toJSONString(oneFieldRange));
                    } else {
                        FixedScore fixedScore = new FixedScore();
                        fixedScore.setValue(FileElseUtil.getObjectToDouble(systemIndex.getIndicatorValue()));
                        systemIndex.setMsgType("fixedScore");
                        systemIndex.setIndexMsg(JSON.toJSONString(fixedScore));
                    }
                }
                systemIndex.setIndicatorType("");
                systemIndexService.updateById(systemIndex);
            }
        }
    }

}
